#include "ultrasonic.h"

extern float Hcsr04GetLength;


void set_nonblocking(int sockfd) {
    int flags = fcntl(sockfd, F_GETFL, 0);
    if (flags < 0) {
        perror("fcntl get");
        return;
    }
    if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) < 0) {
        perror("fcntl set");
    }
}

void send_image(int connfd) {
    FILE *fp;
    char buffer[BUFFER_SIZE] = {0};
    const char *device_path = "/dev/video0"; // 设备路径
    const char *output_file_path = "photo.jpg"; // 输出文件路径
    int width = 640; // 图像宽度
    int height = 480; // 图像高度
    int pixel_format = V4L2_PIX_FMT_MJPEG; // 图像格式

    // 拍照上传
    take_picture(device_path, output_file_path, width, height, pixel_format);
    printf("take photo success: %s\n", output_file_path);

    fp = fopen(output_file_path, "rb");
    if (fp == NULL) {
        perror("fopen");
        return;
    }

    // 获取文件大小
    fseek(fp, 0, SEEK_END);
    uint64_t fileSize = ftell(fp);
    fseek(fp, 0, SEEK_SET);

    // 发送文件大小
    if (send(connfd, &fileSize, sizeof(fileSize), 0) != sizeof(fileSize)) {
        perror("send file size");
        fclose(fp);
        return;
    }

    // 发送文件内容
    size_t n;
    while ((n = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0) {
        if (send(connfd, buffer, n, 0) != n) {
            perror("send file content");
            fclose(fp);
            return;
        }
    }

    fclose(fp);
    printf("File sent successfully\n");

//     // 发送结束标志
//     char endMarker[2] = {0xFF, 0xD9};
//     if (send(connfd, endMarker, sizeof(endMarker), 0) != sizeof(endMarker)) {
//         perror("send end marker");
//     }
}

// void send_image(int connfd) {
//     char buffer[BUFFER_SIZE] = {0};
//     const char *device_path = "/dev/video0"; // 设备路径
//     const char *output_file_path = "photo.jpg"; // 输出文件路径
//     int width = 640; // 图像宽度
//     int height = 480; // 图像高度
//     int pixel_format = V4L2_PIX_FMT_MJPEG; // 图像格式

//     // 拍照上传
//     take_picture(device_path, output_file_path, width, height, pixel_format);
//     printf("take photo success: %s\n", output_file_path);

//     FILE *fp = fopen(output_file_path, "rb");
//     if (fp == NULL) {
//         perror("fopen");
//         return;
//     }

//     // 计算文件大小
//     fseek(fp, 0L, SEEK_END);
//     long file_size = ftell(fp);
//     rewind(fp);

//     // 转换为网络字节序并发送文件大小
//     uint64_t net_file_size = htons((uint64_t)file_size);
//     if (send(connfd, &net_file_size, sizeof(net_file_size), 0) != sizeof(net_file_size)) {
//         perror("send file size");
//         fclose(fp);
//         return;
//     }
//     printf("Sending image of size: %ld bytes\n", file_size);

//     // 发送图像数据
//     size_t n;
//     while ((n = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0) {
//         if (send(connfd, buffer, n, 0) != n) {
//             perror("send image data");
//             break;
//         }
//     }

//     printf("File sent successfully\n");

//     fclose(fp);
// }

// void send_image(int connfd){
//     //设置拍照
    
//     //set_nonblocking(connfd);
    
//     FILE *fp;
//         char buffer[BUFFER_SIZE] = {0};
//         const char *device_path = "/dev/video0"; // 设备路径
//         const char *output_file_path = "photo.jpg"; // 输出文件路径
//         int width = 640; // 图像宽度
//         int height = 480; // 图像高度
//         int pixel_format = V4L2_PIX_FMT_MJPEG; // 图像格式
//         int serial_fd =serial_open("dev/ttySAC2");
//         if (serial_fd < 0) {
//             perror("open serial");
//             return;
//         }
//         serial_config(serial_fd);
//     //拍照上传
//     //cam.c
//     take_picture(device_path, output_file_path, width, height, pixel_format);
//     printf("take photo success: %s\n", output_file_path);
//     fp = fopen("photo.jpg", "rb");
//     if (fp == NULL) {
//         perror("fopen");
//             return;
//     }
//     // 计算文件大小
//     fseek(fp, 0L, SEEK_END);
//     long file_size = ftell(fp);
//     rewind(fp);

//     // 发送文件大小
//     if (send(connfd, &file_size, sizeof(file_size), 0) != sizeof(file_size)) {
//         perror("send file size");
//         fclose(fp);
//         return;
//     }

//     printf("Sending image of size: %ld bytes\n", file_size);

//     // 发送图像数据
//     // char buffer[BUFFER_SIZE] = {0};
//     size_t n;
//     while ((n = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0) {
//         if (send(connfd, buffer, n, 0) != n) {
//             perror("send image data");
//             break;
//         }
//     }    
//     printf("File sent successfully\n");    
//         //serial_close(sfd);

// }


// void send_image(int connfd){
// 	//设置拍照
	
// 	//set_nonblocking(connfd);
	
// 	FILE *fp;
//     	char buffer[BUFFER_SIZE] = {0};
//     	const char *device_path = "/dev/video0"; // 设备路径
//     	const char *output_file_path = "photo.jpg"; // 输出文件路径
//     	int width = 640; // 图像宽度
//     	int height = 480; // 图像高度
//     	int pixel_format = V4L2_PIX_FMT_MJPEG; // 图像格式
//     	int serial_fd =serial_open("dev/ttySAC2");
//     	if (serial_fd < 0) {
//         	perror("open serial");
//         	return;
//     	}
//     	serial_config(serial_fd);
// 	//拍照上传
// 	//cam.c
// 	take_picture(device_path, output_file_path, width, height, pixel_format);
// 	printf("take photo success: %s\n", output_file_path);
// 	fp = fopen("photo.jpg", "rb");
// 	if (fp == NULL) {
// 		perror("fopen");
// 			return;
// 	}
// 	// 发送数据
// 	size_t n;
// 	while ((n = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0) {
// 		send(connfd, buffer, n, 0);
// 	}		
// 	printf("File sent successfully\n");	
//     	//serial_close(sfd);


// }

#if 0
void send_image(int connfd){
	//设置拍照
	
	//set_nonblocking(connfd);
	
	FILE *fp;
    	char buffer[BUFFER_SIZE] = {0};
    	const char *device_path = "/dev/video0"; // 设备路径
    	const char *output_file_path = "photo.jpg"; // 输出文件路径
    	int width = 640; // 图像宽度
    	int height = 480; // 图像高度
    	int pixel_format = V4L2_PIX_FMT_MJPEG; // 图像格式
    	int serial_fd =serial_open("dev/ttySAC2");
    	if (serial_fd < 0) {
        	perror("open serial");
        	return;
    	}
    	serial_config(serial_fd);

	#if 0
	//读距离
	char buf[1024];
    	int sfd=serial_open("/dev/ttyUSB0");
    	if(sfd==-1)
    	{
        	perror("open");
        	return;
    	}
    	if(serial_config(sfd)!=0)
    	{
        	perror("serial configure");
        	serial_close(sfd);
        	return;
    	}
    	#endif
    	float k = 0;
    	int flag = 1;
    	while(1)
    	{	
    		#if 0
    		static int i = 0;
    		memset(buf, 0, sizeof(buf));
        	int ret=serial_read_image(sfd,buf);
        	if(ret==-1)
        	{
			perror("read");
        		printf("Error number: %d\n", errno);
        		printf("Error string: %s\n", strerror(errno));
        		printf("here\n");
            		return;
        	}
    			//判断距离
	    		//distance = change(buf);
	    	#endif
	    	//printf("distance = %f\n", Hcsr04GetLength);
	    	if(Hcsr04GetLength < 5.0 && flag){
	    		k = Hcsr04GetLength;
	    		flag = 0;
	    		//拍照上传
	    		//cam.c
	    		take_picture(device_path, output_file_path, width, height, pixel_format);
			printf("take photo success: %s\n", output_file_path);
			fp = fopen("photo.jpg", "rb");
			if (fp == NULL) {
				perror("fopen");
				return;
			}
			// 发送数据
			size_t n;
			while ((n = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0) {
				send(connfd, buffer, n, 0);
			}
			
			printf("File sent successfully\n");	
	    	}
	    	if(Hcsr04GetLength - k > 10 || Hcsr04GetLength - k < -10){
	    			flag = 1;
	    	}
	    	// 接收数据
	    	#if 0
		int read_buf = recv(connfd, buf, sizeof(buf) - 1, 0);
		if (read_buf < 0) {
		    if (errno == EAGAIN || errno == EWOULDBLOCK) {
		        // 没有数据可读，继续循环
		        continue;
		    } 
		    else {
		        perror("recv");
		        printf("here\n");
		        //close(connfd);
		        return;
		    }
		} 
		else if (read_buf == 0) {
		    // 连接关闭
		    printf("Connection closed by peer\n");
		    //close(connfd);
		    return;
		}
		buf[read_buf] = '\0'; // 确保字符串结束
		if (strcmp(buf, "open_sg90") == 0) {
		    printf("buf = %s\n", buf);
		    continue;
		} 
		else {
		    printf("error\n");
		}
		#endif
    	}
    	//serial_close(sfd);


}
#endif
